package com.atguigu.aop.annotation.aspect;

import com.atguigu.aop.annotation.utils.LoggerUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * Date:2020/9/25
 * Author:ybc
 * Description:
 */
@Component
@Aspect
@Order(-1)
public class LoggerAspect {

    @Pointcut("execution(* com.atguigu.aop.annotation.target.*.*(..))")
    public void publicPointCut(){}

    //@Before("execution(public int com.atguigu.aop.annotation.target.MathImpl.add(int,int))")
    //@Before("execution(* com.atguigu.aop.annotation.target.*.*(..))")
    @Before("publicPointCut()")
    public void a(JoinPoint joinPoint){
        LoggerUtils.showMethodNameAndArgs(joinPoint.getSignature().getName(), joinPoint.getArgs());
    }

    @After("publicPointCut()")
    public void afterAdviceMethod(){
        System.out.println("后置通知-->finally");
    }

    @AfterReturning(value = "publicPointCut()", returning = "result")
    public void afterReturningAdviceMethod(JoinPoint joinPoint, Object result){
        LoggerUtils.showMethodNameAndResult(joinPoint.getSignature().getName(), result);
    }

    @AfterThrowing(value = "publicPointCut()", throwing = "ex")
    public void afterThrowingAdviceMethod(JoinPoint joinPoint, Exception ex){
        LoggerUtils.showMethodNameAndExceptionMessage(joinPoint.getSignature().getName(), ex.getMessage());
    }

    //@Around("publicPointCut()")
    public Object aroundAdviceMethod(ProceedingJoinPoint joinPoint){
        Object object = null;
        try {
            System.out.println("Around-->前置");
            object = joinPoint.proceed();
            System.out.println("Around-->返回通知");
        } catch (Throwable throwable) {
            System.out.println("Around-->异常通知");
            throwable.printStackTrace();
        } finally {
            System.out.println("Around-->后置通知");
        }
        return object;
    }

}
